package com.zhu.storesmanager.views;

import java.util.ArrayList;
import java.util.List;

import com.zhu.storesmanager.R;
import com.zhu.storesmanager.SvStoreCompeteActivity;
import com.zhu.storesmanager.models.BrandsDataItem;
import com.zhu.storesmanager.models.CompeteProductItem;
import com.zhu.storesmanager.models.DataItem;
import com.zhu.storesmanager.utils.Actions;
import com.zhu.storesmanager.utils.DataUtils;
import com.zhu.storesmanager.utils.DatabaseUtil;
import com.zhu.storesmanager.utils.ViewsUtil;
import com.zhu.storesmanager.views.ChoiceView.DataChangeListener;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * 竞品产品
 */
public class CompeteProductsView extends LinearLayout implements OnClickListener{

    private ChoiceView mBrandChoiceview = null; //品牌
    private ChoiceView mModelChoiceview = null; //型号
    private EditText mPriceEditText = null; //价格
    private LinearLayout mBodyLinearLayout = null; //新增项
    private List<CompeteProductItem> mDataList = new ArrayList<CompeteProductItem>();
    private List<DataItem> mAllModelsList = null; //所有型号数据
    private List<DataItem> mModelsList = null; //型号数据
    private CheckBox mCheckBox = null; //是否狗选

    public CompeteProductsView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public CompeteProductsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CompeteProductsView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    /**
     * 界面加载完成
     * (non-Javadoc)
     * @see android.view.View#onFinishInflate()
     */
    public void onFinishInflate(){
        mBrandChoiceview = ViewsUtil.setChoiceView(findViewById(R.id.compete_brands), R.string.sales_brands);
        mModelChoiceview = ViewsUtil.setChoiceView(findViewById(R.id.compete_model), R.string.model);
        mPriceEditText = ViewsUtil.setEditTextView(findViewById(R.id.compete_price), R.string.price);
        mCheckBox = (CheckBox)findViewById(R.id.compete_price_label); 
        findViewById(R.id.compete_newadd).setOnClickListener(this);
        mBodyLinearLayout = (LinearLayout)findViewById(R.id.compete_body);
        if(DataUtils.mUserData.isAdmin()){ //管理员隐藏输入
        	findViewById(R.id.operate).setVisibility(View.GONE);
        	findViewById(R.id.compete_brands).setVisibility(View.GONE);
        	findViewById(R.id.compete_model).setVisibility(View.GONE);
        	findViewById(R.id.compete_price).setVisibility(View.GONE);
        	findViewById(R.id.compete_newadd).setVisibility(View.GONE);
        }
    }

    /**
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v != null){
            switch(v.getId()){
                case R.id.compete_newadd: //新增
                    addCompeteProductData();
                    break;
                case R.id.brand_delete:
                    deleteData((CompeteProductItem)v.getTag()); //删除
                    break;
            }
        }
    }

    /**
     * 设置选项数据
     * @param brandsList 品牌选项
     * @param modelsList 型号选项
     */
    public void setChoiceviewList(List<DataItem> brandsList, List<DataItem> modelsList){
        mBrandChoiceview.setChoiceDataList(brandsList);
        mBrandChoiceview.setDataChangeListener(mDataChangeListener);
        mAllModelsList = modelsList;
        //mModelChoiceview.setChoiceDataList(modelsList);
    }

    /**
     * 添加竞品型号加个数据界面
     * @param dataItem 品牌数据
     */
    private void addDataView(CompeteProductItem dataItem){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_bandinfo_item, null);
        view.setOnClickListener(this);
        view.setTag(dataItem);
        ViewsUtil.setTextViewData(view, R.id.brand_name, dataItem.mBrandName);
        ViewsUtil.setTextViewData(view, R.id.brand_sellcount, dataItem.mModelName);
        ViewsUtil.setTextViewData(view, R.id.brand_sellmoney, dataItem.mPrice);
        View delete = view.findViewById(R.id.brand_delete);
        if(DataUtils.mUserData.isAdmin()){
        	delete.setVisibility(View.GONE);
        }else{
        	delete.setOnClickListener(this);
            delete.setTag(dataItem);
        }
        mBodyLinearLayout.addView(view);
    }

    /**
     * 添加品牌数据
     */
    private void addCompeteProductData(){
        String brandID = mBrandChoiceview.getSaveData();
        String modelID = mModelChoiceview.getSaveData();
        String price = mPriceEditText.getText().toString().trim();
        if(brandID != null && brandID.length() > 0 && modelID != null && modelID.length() > 0 && price != null && price.length() > 0){
            CompeteProductItem item = new CompeteProductItem();
            item.mBrandId = brandID;
            item.mBrandName = mBrandChoiceview.getSelectedText();
            item.mModelID = modelID;
            item.mModelName = mModelChoiceview.getSelectedText();
            item.mPrice = price;
            addCompeteProductData(item, true);
        }else{
            Actions.toastShort(getContext(), R.string.compete_price_notnull);
        }
    }

    /**
     * 添加品牌数据
     * @param dataItem 竞品产品数据
     * @param isCheck 是否检测重复数据
     */
    public void addCompeteProductData(CompeteProductItem dataItem, boolean isCheck){
        if(isCheck && checkDatadDuplication(dataItem)){
            Actions.toastShort(getContext(), R.string.compete_data_duplication);
            return;
        }
        mDataList.add(dataItem);
        addDataView(dataItem);
    }


    /**
     * 删除数据项
     * @param dataItem 竞品产品数据
     */
    private void deleteData(CompeteProductItem dataItem){
        int index = mDataList.indexOf(dataItem);
        mDataList.remove(index);
        mBodyLinearLayout.removeViewAt(index);
    }

    /**
     * 检查数据是否重复
     * @param dataItem 竞品产品数据
     * @param true 数据重复 否则反之
     */
    private boolean checkDatadDuplication(CompeteProductItem dataItem){
        for(CompeteProductItem item : mDataList){
            if(item.mBrandId.equals(dataItem.mBrandId) && item.mModelID.equals(dataItem.mModelID)){
                return true;
            }
        }
        return false;
    }

    /**
     * 保存数据
     * @param storeId 门店ID
     */
    public void saveData(String storeId){
        if(mDataList.size() > 0){
            DatabaseUtil.saveCompeteData(getContext(), mDataList, storeId, SvStoreCompeteActivity.TYPE_COMPETE_CONTENT_PRICE);
        }
    }

    /**
     * 设置标签文本
     * @param text
     */
    public void setLableText(String text){
    	mCheckBox.setText(text);
    }

    /**
     * 品牌数据发生变化监听
     */
    DataChangeListener mDataChangeListener = new DataChangeListener(){
		@Override
		public void onDataChange(ChoiceView view) {
			// TODO Auto-generated method stub
			updateModelList(view.getSelectedText());
		}
    };

    /**
     * 根据品牌更新型号选择列表
     */
    private void updateModelList(String brandName){
    	if(mAllModelsList != null && mAllModelsList.size() > 0 && brandName != null && brandName.length() > 0){
    		if(mModelsList == null){
    			mModelsList = new ArrayList<DataItem>();
    		}
    		mModelsList.clear();
    		for(DataItem dataItem : mAllModelsList){
    			String[] arr = dataItem.mDataName.split(";");
    			if(arr[0].equals(brandName)){
    				DataItem item = new DataItem();
    				item.mDataId = dataItem.mDataId;
    				item.mDataName = arr[1];
    				item.mDataType = dataItem.mDataType;
    				mModelsList.add(item);
    			}
    		}
    		mModelChoiceview.setChoiceDataList(mModelsList);
    	}
    }

    /**
     * 是不是空数据
     * @param isShow 是否显示
     */
    public boolean checkData(boolean isShow){
    	if(isShow && mCheckBox.isChecked()){
    		if(mDataList != null && mDataList.size() > 0){
    			return true;
    		}else{
    			return false;
    		}
    	}
    	return true;
    }

    /**
     * 是否选中
     */
    public boolean isSelect(){
    	return mCheckBox.isChecked();
    }

    /**
     * 设置选中状态
     */
    public void setSelectStatus(boolean select){
    	mCheckBox.setChecked(select);
    }
}
